package com.whfc.ms.api.emp.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.emp.dto.*;
import com.whfc.emp.param.*;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.emp.service.MsAppEmpAttendService;
import com.whfc.ms.api.emp.service.MsAppEmpInputDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 人员考勤
 *
 * @author guodong_sun
 * @Description:
 * @date 2019年8月27日
 */
@RestController
@RequestMapping("/ms/api/emp")
public class AppEmpAttendController extends BaseController {

    @Autowired
    private MsAppEmpAttendService msAppEmpAttendService;

    @Autowired
    private MsAppEmpInputDataService msAppEmpInputDataService;
    /**
     * 人员管理-日考勤记录列表
     *
     * @param user
     * @param pageNum
     * @param pageSize
     * @param date
     * @param deptId
     * @param keyword
     * @return
     */
    @GetMapping("/attend/day/list")
    public Result attendDayList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                @RequestParam("pageNum") Integer pageNum,
                                @RequestParam("pageSize") Integer pageSize,
                                @RequestParam(value = "date", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
                                @RequestParam(value = "deptId") Integer deptId,
                                @RequestParam(value = "corpId", required = false) Integer corpId,
                                @RequestParam(value = "groupId", required = false) Integer groupId,
                                @RequestParam(value = "keyword", required = false) String keyword,
                                @RequestParam(value = "attendState", required = false) Integer attendState) {
        logger.info("人员管理-日考勤记录列表,loginer:{}, pageNum:{}, pageSize:{}, date:{}, deptId:{}, corpId:{}, " +
                        "groupId:{}, keyword:{}, attendState:{}",
                user.getUsername(), pageNum, pageSize, date, deptId, corpId, groupId, keyword, attendState);
        PageData<AttendDayEmpDTO> data = msAppEmpAttendService.attendDayList(pageNum, pageSize, date, deptId, corpId,
                groupId, keyword, attendState);
        return ResultUtil.success(data);
    }


    @GetMapping("/attend/day/num")
    public Result attendDayNum(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                               @RequestParam(value = "date", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
                               @RequestParam(value = "deptId") Integer deptId,
                               @RequestParam(value = "corpId", required = false) Integer corpId,
                               @RequestParam(value = "groupId", required = false) Integer groupId,
                               @RequestParam(value = "keyword", required = false) String keyword,
                               @RequestParam(value = "empId", required = false) Integer empId) {
        logger.info("人员管理-日考勤记录统计,loginer:{},date:{},deptId:{},corpId:{},groupId:{},keyword:{},empId:{}",
                user.getUsername(), date, deptId, corpId, groupId, keyword, empId);
        AppEmpAttendDayNumDTO appEmpAttendDayNumDTO = msAppEmpAttendService.attendDayNum(date, deptId, corpId, groupId, keyword, empId);
        return ResultUtil.success(appEmpAttendDayNumDTO);
    }


    /**
     * 人员管理-月考勤记录列表
     *
     * @param user
     * @param pageNum
     * @param pageSize
     * @param time
     * @param deptId
     * @param keyword
     * @return
     */
    @GetMapping("/attend/month/list")
    public Result attendMonthList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                  @RequestParam("pageNum") Integer pageNum,
                                  @RequestParam("pageSize") Integer pageSize,
                                  @RequestParam("time") @DateTimeFormat(pattern = "yyyy-MM") Date time,
                                  @RequestParam(value = "deptId") Integer deptId,
                                  @RequestParam(value = "corpId", required = false) Integer corpId,
                                  @RequestParam(value = "groupId", required = false) Integer groupId,
                                  @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info(" 人员管理-月考勤记录列表,loginer:{},pageNum:{},pageSize:{},time:{},deptId:{},corpId:{},groupId:{}keyword:{}",
                user.getUsername(), pageNum, pageSize, time, deptId, corpId, groupId, keyword);
        PageData<AppEmpAttendMonthDTO> pageData = msAppEmpAttendService.attendMonthList(pageNum, pageSize, time, deptId, corpId, groupId, keyword);
        return ResultUtil.success(pageData);
    }


    /**
     * 人员管理-gps记录
     *
     * @param user
     * @param empId
     * @param date
     * @return
     */
    @GetMapping("/attend/gps/list")
    public Result attendGpsList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                @RequestParam("empId") Integer empId,
                                @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
        logger.info("人员管理-gps记录,loginer:{},empId:{},date:{}", user.getUsername(), empId, date);
        List<AppEmpGpsDTO> list = msAppEmpAttendService.attendGpsList(empId, date);
        return ResultUtil.success(new ListData<>(list));
    }


    /**
     * 人员管理-人脸识别记录
     *
     * @param user
     * @param pageNum
     * @param pageSize
     * @param deptId
     * @param keyword
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/attend/faceGate/list")
    public Result attendFaceGateList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                     @RequestParam("pageNum") Integer pageNum,
                                     @RequestParam("pageSize") Integer pageSize,
                                     @RequestParam(value = "deptId") Integer deptId,
                                     @RequestParam(value = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                     @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
                                     @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info(" 人员管理-人脸识别记录,loginer:{},pageNum:{},pageSize:{},deptId:{},keyword:{},startTime:{},endTime:{}",
                user.getUsername(), pageNum, pageSize, deptId, keyword, startTime, endTime);
        PageData<AppFaceGateRecordDTO> pageData = msAppEmpAttendService.attendFaceGateList(pageNum, pageSize, deptId, keyword, startTime, endTime);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/attend/faceGate/discernList")
    public Result attendFaceGateList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                     @RequestParam(value = "deptId") Integer deptId,
                                     @RequestParam(value = "type",required = false) Integer type,
                                     @RequestParam(value = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                     @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        logger.info(" 实名制大屏-访客记录,loginer:{},deptId:{},startTime:{},endTime:{}",
                user.getUsername(),  deptId, startTime, endTime);
        List<AppFaceGateRecordDTO> pageData = msAppEmpAttendService.discernList( deptId,  startTime, endTime,type);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/attend/clockList")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "startDate", required = false) Date startDate,
                       @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "endDate", required = false) Date endDate) {
        logger.info("打卡记录列表,loginer:{},deptId:{},startDate:{},endDate:{}",
                user.getUsername(), deptId, startDate, endDate);
        List<AppEmpInputDataDTO> pageData = msAppEmpInputDataService.list(deptId,  startDate, endDate);
        return ResultUtil.success(pageData);
    }


    @GetMapping("/attend/faceGate/temperatureAbnormalList")
    public Result attendFaceGateList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                     @RequestParam(value = "deptId") Integer deptId,
                                     @RequestParam(value = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                     @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        logger.info(" 实名制大屏-温度异常记录,loginer:{},deptId:{},startTime:{},endTime:{}",
                user.getUsername(),  deptId,startTime,endTime);
        List<AppFaceGateRecordDTO> pageData = msAppEmpAttendService.temperatureAbnormalList(deptId,startTime,endTime);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/attend/faceGate/visitor")
    public Result visitorList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                              @RequestParam("pageNum") Integer pageNum,
                              @RequestParam("pageSize") Integer pageSize,
                              @RequestParam(value = "deptId") Integer deptId,
                              @RequestParam(value = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
                              @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime) {
        logger.info("人员管理-访客记录,loginer:{},pageNum:{},pageSize:{},deptId:{},startTime:{},endTime:{}",
                user.getUsername(), pageNum, pageSize, deptId, startTime, endTime);
        PageData<AppFaceGateRecordDTO> pageData = msAppEmpAttendService.visitorList(pageNum, pageSize, deptId, startTime, endTime);
        return ResultUtil.success(pageData);
    }

    @PostMapping("/attend/faceGate/visitorListExport")
    public Result visitorListExport(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                    @RequestParam(value = "deptId") Integer deptId,
                                    @RequestParam(value = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
                                    @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime) throws Exception {
        logger.info("人员管理-智能闸机-导出访客进出记录,loginer:{},deptId:{},startTime:{},endTime:{}", user.getUsername(),  deptId, startTime, endTime);
        OssPathDTO data = msAppEmpAttendService.visitorListExport(deptId,startTime,endTime);
        return ResultUtil.success(data);
    }


    @PostMapping("/attend/faceGate/export")
    public Result faceGateRecordExport(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                       @Validated @RequestBody FaceGateRecordExportParam param) throws Exception {
        logger.info("导出识别记录,loginer:{},param:{}", user.getUsername(), param.toString());
        OssPathDTO data = msAppEmpAttendService.faceGateRecordExport(param);
        return ResultUtil.success(data);
    }

    /**
     * 考勤方式的设置
     *
     * @param user
     * @param request
     * @return
     */
    @RequiresPermissions("emp/rule/attendSet")
    @PostMapping("/attend/typeSet")
    public Result attendTypeSet(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                @RequestBody @Validated AppEmpAttendTypeSetParam request) {
        logger.info("考勤方式设置,loginer:{},param:{}", user.getUsername(), request.toString());
        msAppEmpAttendService.setAttendType(request);
        return ResultUtil.success();
    }

    /**
     * 获取组织机构的考勤方式
     *
     * @param user
     * @param deptId
     * @return
     */
    @GetMapping("/getAttendType")
    public Result getAttendType(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                @RequestParam("deptId") Integer deptId) {
        logger.info("获取组织机构的考勤方式,loginer:{},deptId:{}", user.getUsername(), deptId);
        AppEmpAttendTypeDTO typeDTO = msAppEmpAttendService.getAttendType(deptId);
        return ResultUtil.success(typeDTO);
    }

    @PostMapping("/attend/month/export")
    public Result exportMonthReport(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                    @Validated @RequestBody EmpMonthReportExportParam request) throws Exception {
        logger.info("导出人员月考勤记录,loginer:{},param:{}", user.getUsername(), request.toString());
        OssPathDTO ossPathDTO = msAppEmpAttendService.exportMonthReport(request);
        return ResultUtil.success(ossPathDTO);
    }

    @PostMapping("/attend/day/export")
    public Result exportDayReport(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                  @Validated @RequestBody EmpDayReportExportParam request) throws Exception {
        logger.info("导出人员日考勤记录,loginer:{},param:{}", user.getUsername(), request.toString());
        OssPathDTO ossPathDTO = msAppEmpAttendService.exportDayReport(request);
        return ResultUtil.success(ossPathDTO);
    }

    @GetMapping("/attend/record")
    public Result attendRecord(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                               @RequestParam("empId") Integer empId,
                               @RequestParam("pageNum") Integer pageNum,
                               @RequestParam("pageSize") Integer pageSize,
                               @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date,
                               @RequestParam(value = "state", required = false) Integer state) {
        logger.info("打卡记录,loginer:{},empId:{},date:{},pageNum:{},pageSize:{},state:{}",
                user.getUsername(), empId, date, pageNum, pageSize, state);
        PageData<AppEmpAttendRecordDTO> pageData = msAppEmpAttendService.attendRecord(empId, date, pageNum, pageSize, state);
        return ResultUtil.success(pageData);
    }

    @PostMapping("/attend/anon/day/syncRecord")
    public Result syncRecord(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                  @Validated @RequestBody AppEmpAttendSyncParam request) {
        logger.info("同步考勤打卡时间,loginer:{},param:{}", user.getUsername(), request.toString());
        msAppEmpAttendService.syncRecord(request);
        return ResultUtil.success();
    }
}
